package com.traffic.fuwu.controller;

import com.traffic.fuwu.mybatis.pojo.Account;
import com.traffic.fuwu.mybatis.pojo.AlipayEnterpriseAccount;
import com.traffic.fuwu.mybatis.pojo.Company;
import com.traffic.fuwu.mybatis.pojo.Sftp;
import com.traffic.fuwu.service.AccountService;
import com.traffic.fuwu.service.CompanyService;
import com.traffic.fuwu.service.SftpService;
import com.traffic.fuwu.util.AppKeyUtil;
import com.traffic.fuwu.util.MD5Util;
import com.traffic.fuwu.util.Page;
import com.traffic.fuwu.util.ToolUtil;
import com.traffic.fuwu.vo.VoStatus;
import javafx.beans.binding.ObjectExpression;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pikewei on 2017/2/7.
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    private static final Logger log = LoggerFactory.getLogger(CompanyController.class);
    private static final VoStatus voStatus = new VoStatus();

    @Autowired
    private CompanyService companyService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private SftpService sftpService;

    /**
     * 多条件联合查询公司信息
     *
     * @param comName
     * @param comAddress
     * @param pageIndex
     * @param comCode
     * @return
     */
    @RequestMapping(value = "/companyList")
    @ResponseBody
    public Map<String, Object> listCompany(String comName, String comAddress, String pageIndex, String comCode) {
        Map<String, Object> map = new HashMap<>();
        String pageSize = "10";// 每一页的行数
        if (StringUtils.isEmpty(pageIndex)) {
            pageIndex = "1";
        }
        Page page = new Page();
        page.setPageIndex(Integer.parseInt(pageIndex));
        page.setPageSize(Integer.parseInt(pageSize));

        try {
            if (comName != null && comName != "") {
                comName = URLDecoder.decode(comName, "UTF-8");
            }
            if (comAddress != null && comAddress != "") {
                comAddress = URLDecoder.decode(comAddress, "UTF-8");
            }
            if (StringUtils.isNotBlank(comCode)) {
                comCode = URLDecoder.decode(comCode, "UTF-8");
            }
            //查询总数量
            int count = companyService.getFindComCount(comName, comAddress, comCode);
            page = ToolUtil.getPage(count, page);

            List<Map<String, Object>> list = companyService.getFindCom(comName, comAddress, comCode, page.startRow(),
                    page.getPageSize());
            if (list != null && list.size() != 0) {
                map.put("line", list);
                voStatus.setCode(1);
                voStatus.setMessage("查询成功");
                map.put("page", page);
                map.put("status", voStatus);
            }
            voStatus.setCode(0);
            voStatus.setMessage("查无数据");
            map.put("status", voStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 根据id查询
     *
     * @param comId
     * @return
     */
    @RequestMapping(value = "/getById")
    @ResponseBody
    public Map<String, Object> getById(int comId) {
        Map<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> temp = companyService.getById(comId);
            if (temp != null && temp.size() != 0) {
                map.put("line", temp);
                voStatus.setCode(1);
                voStatus.setMessage("查询成功");
                map.put("status", voStatus);
            }
            voStatus.setCode(0);
            voStatus.setMessage("查无数据");
            map.put("status", voStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 新增公司信息
     *
     * @param accountName
     * @param accountPassword
     * @param account
     * @param passwd
     * @param accountChinese
     * @param comAddress
     * @param comName
     * @param comCode
     * @return
     */
    @RequestMapping(value = "/addCompany")
    @ResponseBody
    public Map<String, Object> addCompany(String accountName, String accountPassword, String account, String passwd,
                                          String accountChinese, String comAddress, String comName, String comCode
            , String appId, String privateKey) {
        Map<String, Object> map = new HashMap<>();
        try {
            do {
                if (!StringUtils.isNotBlank(comCode)) {
                    voStatus.setCode(0);
                    voStatus.setMessage("增加失败，无效comCode");
                    map.put("status", voStatus);
                    break;
                }
                Company company = null;
                Account account1 = null;
                Sftp sftp = null;
                AlipayEnterpriseAccount alipayEnterpriseAccount = null;
                String appKey = AppKeyUtil.getAppKey();
                if (!StringUtils.isAnyBlank(accountName, accountPassword, accountChinese)) {

                    account1 = accountService.getByAccountName(accountName);
                    if (account1 != null) {
                        voStatus.setCode(0);
                        voStatus.setMessage("增加失败，accountName已存在");
                        map.put("status", voStatus);
                        break;
                    }
                    account1 = new Account();
                    accountChinese = URLDecoder.decode(accountChinese, "UTF-8");
                    account1.setAccountChinese(accountChinese);
                    account1.setAccountName(accountName);
                    account1.setAccountPassword(MD5Util.GetMD5Code(accountPassword));
                    account1.setComCode(comCode + "-" + appKey);
                    account1.setAppKey(appKey);
                }
                if (!StringUtils.isAnyBlank(account, passwd)) {
                    sftp = sftpService.getByAccount(account);
                    if (sftp != null) {
                        voStatus.setCode(0);
                        voStatus.setMessage("增加失败，account已存在");
                        map.put("status", voStatus);
                        break;
                    }
                    sftp = new Sftp();
                    sftp.setAccount(account);
                    sftp.setPasswd(passwd);
                    sftp.setComCode(comCode + "-" + appKey);
                    sftp.setAppKey(appKey);
                }
                if (!StringUtils.isAnyBlank(comName, comAddress)) {
                    company = new Company();
                    comName = URLDecoder.decode(comName, "UTF-8");
                    comAddress = URLDecoder.decode(comAddress, "UTF-8");
                    company.setComName(comName);
                    company.setComCode(comCode + "-" + appKey);
                    company.setComParentCode(comCode);
                    company.setComAddress(comAddress);
                    company.setAppKey(appKey);
                }
                if(!StringUtils.isAnyBlank(appId,privateKey)){
                    alipayEnterpriseAccount = new AlipayEnterpriseAccount();
                    alipayEnterpriseAccount.setAppKey(appKey);
                    alipayEnterpriseAccount.setCreateDate(new Date());
                    alipayEnterpriseAccount.setAppId(appId);
                    alipayEnterpriseAccount.setPrivateKey(privateKey);
                    alipayEnterpriseAccount.setGateWay("https://openapi.alipay.com/gateway.do");
                    alipayEnterpriseAccount.setStringCharset("GBK");
                }
                companyService.addCompany(company, sftp, account1,alipayEnterpriseAccount);
                voStatus.setCode(1);
                voStatus.setMessage("增加成功");
                map.put("status", voStatus);
            } while (false);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 修改公司信息
     *
     * @param comId
     * @param accountId
     * @param sftpId
     * @param accountName
     * @param accountPassword
     * @param account
     * @param passwd
     * @param accountChinese
     * @param comAddress
     * @param comName
     * @return
     */
    @RequestMapping(value = "/updateCompany")
    @ResponseBody
    public Map<String, Object> updateCompany(String comId, String accountId, String sftpId, String accountName,
                                             String accountPassword, String account, String passwd,
                                             String accountChinese, String comAddress, String comName) {
        Map<String, Object> map = new HashMap<>();
        try {
            do {
                if (StringUtils.isNotBlank(comId)) {
                    Company company = companyService.getUpdateById(Integer.parseInt(comId));
                    if (StringUtils.isNotBlank(comAddress)) {
                        comAddress = URLDecoder.decode(comAddress, "UTF-8");
                        company.setComAddress(comAddress);
                    }
                    if (StringUtils.isNotBlank(comName)) {
                        comName = URLDecoder.decode(comName, "UTF-8");
                        company.setComName(comName);
                    }
                    companyService.updateCompany(company);
                }
                if (StringUtils.isNotBlank(accountId)) {
                    Account account1 = accountService.getById(Integer.parseInt(accountId));
                    if (StringUtils.isNotBlank(accountName)) {
                        account1.setAccountName(accountName);
                    }
                    if (StringUtils.isNotBlank(accountPassword)) {
                        account1.setAccountPassword(MD5Util.GetMD5Code(accountPassword));
                    }
                    if (StringUtils.isNotBlank(accountChinese)) {
                        accountChinese = URLDecoder.decode(accountChinese, "UTF-8");
                        account1.setAccountChinese(accountChinese);
                    }
                    companyService.updateAccount(account1);
                }
                if (StringUtils.isNotBlank(sftpId)) {
                    Sftp sftp = sftpService.getById(Integer.parseInt(sftpId));
                    if (StringUtils.isNotBlank(account)) {
                        sftp.setAccount(account);
                    }
                    if (StringUtils.isNotBlank(passwd)) {
                        sftp.setPasswd(passwd);
                    }
                    companyService.updateSftp(sftp);
                }
                voStatus.setCode(1);
                voStatus.setMessage("修改成功");
                map.put("status", voStatus);

            } while (false);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;

    }

    /**
     * 删除
     *
     * @param comId
     * @param sftpId
     * @param accountId
     * @return
     */
    @RequestMapping(value = "/deleteCompany")
    @ResponseBody
    public Map<String, Object> deleteCompany(String comId, String sftpId, String accountId) {
        Map<String, Object> map = new HashMap<>();
        try {
            do {
                if (!StringUtils.isNotBlank(comId) && !StringUtils.isNotBlank(sftpId) && !StringUtils.isNotBlank(accountId)) {
                    voStatus.setCode(0);
                    voStatus.setMessage("删除失败,无法获取需删除信息！");
                    map.put("status", voStatus);
                    break;
                }
                if (StringUtils.isNotBlank(comId)) {
                    companyService.deleteCompany(Integer.parseInt(comId));
                }
                if (StringUtils.isNotBlank(sftpId)) {
                    companyService.deleteSftp(Integer.parseInt(sftpId));
                }
                if (StringUtils.isNotBlank(accountId)) {
                    companyService.deleteAccount(Integer.parseInt(accountId));
                }
                voStatus.setCode(1);
                voStatus.setMessage("删除成功");
                map.put("status", voStatus);
            } while (false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
